import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_wan_android/arch/net/http_manager.dart';
import 'package:flutter_wan_android/arch/net/result_data.dart';
import 'package:flutter_wan_android/event/event.dart';
import 'package:flutter_wan_android/event/event_bus.dart';
import 'package:flutter_wan_android/model/article.dart';
import 'package:flutter_wan_android/res/theme.dart';
import 'package:flutter_wan_android/widget/article_item.dart';
import 'package:flutter_wan_android/widget/refresh.dart';
import 'package:fluttertoast/fluttertoast.dart';

class SystemTxDetail extends StatefulWidget {
  int id = 0;

  SystemTxDetail(this.id);

  @override
  _SystemTxDetailState createState() => _SystemTxDetailState(id);
}

class _SystemTxDetailState extends State<SystemTxDetail>
    with AutomaticKeepAliveClientMixin {
  int id = 0;

  List<Article> _articles = [];

  EasyRefreshController _controller = EasyRefreshController();

  var _pageIndex = 0;

  _SystemTxDetailState(this.id);

  @override
  void initState() {
    super.initState();
    _getArticles();

    EventBusUtil.getInstance().on<String>().listen((event) {
      if(event == WEvent.EVENT_LOGIN){
        _pageIndex = 0;
        _getArticles();
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    EventBusUtil.getInstance().destroy();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: wTheme.background,
      child: EasyRefresh(
        controller: _controller,
        enableControlFinishLoad: true,
        firstRefresh: true,
        enableControlFinishRefresh: true,
        header: refreshHeader,
        footer: refreshFooter,
        child: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            if (_articles.isEmpty) return Container();
            return ArticleItem( _articles[index],
                onRefresh: () {
                  setState(() {
                    List<Article> newList = [];
                    newList.addAll(_articles);
                    newList[index].collect =
                    !(newList[index].collect ?? false);
                    _articles = newList;
                  });
                });
          },
          itemCount: _articles.length,
        ),
        onRefresh: () async {
          _pageIndex = 0;
          _getArticles();
        },
        onLoad: () async {
          _getArticles();
        },
      ),
    );
  }

  /// 获取文章列表
  _getArticles() async {
    ResultData result = await HttpManager.instance()
        .get("article/list/${_pageIndex}/json?cid=${id}", withLoading: false);

    _controller.finishRefresh();
    bind(_controller, result);

    if (result.success) {
      setState(() {
        if (_pageIndex == 0) {
          _articles = getArticleList(result.data["datas"]);
        } else {
          _articles.addAll(getArticleList(result.data["datas"]));
        }
      });
      _pageIndex++;
    } else {
      Fluttertoast.showToast(msg: result.msg);
    }
  }

  @override
  bool get wantKeepAlive => true;
}
